package com.jkhan.demo.threads;

/**
 * @ClassName TicketSell2
 * @Description TicketSell2
 * @Author hjk
 * @Date 2022/1/6 21:14
 **/
public class TicketSell2 {
    public static void main(String[] args) {
        Windows2 windows = new Windows2();

        Thread thread1 = new Thread(windows);
        Thread thread2 = new Thread(windows);
        Thread thread3 = new Thread(windows);

        thread1.setName("windows1");
        thread2.setName("windows2");
        thread3.setName("windows3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Windows2 implements Runnable {
    private int tickets = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ": 卖票,票号为: " + tickets);
                    tickets--;
                } else {
                    break;
                }
            }
        }
    }
}
